package gui;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextArea;

import logic.ToGuiFacade;
import data.MovieComputedRating;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class pnlRecommendation extends javax.swing.JPanel {
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private JLabel picLabel;
	private JButton next10Button;
	private JButton backButton;
	private JButton previousButton;
	private JTextArea headlineTextArea;
	private static JLabel[] _movieLabels;
	private static JLabel[] _scoresLabels;
	private static JLabel[] _numLabels;
	private static JButton[] _rateButtons;
	private static String _user;

	public pnlRecommendation(String user) {
		super();
		_user = user;
		initGUI();
	}

	private void initGUI() {
		ToGuiFacade.getInstance().setRecommandationPanel(this);
		try {
			this.setPreferredSize(new java.awt.Dimension(699, 489));
			this.setBackground(new java.awt.Color(255,128,0));
			this.setLayout(null);
			this.setFont(new java.awt.Font("Arial",0,12));
			{
				picLabel = new JLabel();
				this.add(picLabel);
				picLabel.setIcon(new ImageIcon(getClass().getClassLoader().getResource("documentation/miniSecret4PlusDog.JPG")));
				picLabel.setBounds(555, 29, 132, 237);
			}
			{
				headlineTextArea = new JTextArea();
				this.add(headlineTextArea);
				headlineTextArea.setText("These are the movies we recommend you:");
				headlineTextArea.setBounds(70, 30, 426, 27);
				headlineTextArea.setOpaque(false);
				headlineTextArea.setFont(new java.awt.Font("Arial",1,20));
				headlineTextArea.setEditable(false);
			}
			_numLabels = new JLabel[10];
			for(int i=0 ; i<10 ; i++) {
				_numLabels[i] = new JLabel();
				this.add(_numLabels[i]);
				_numLabels[i].setBounds(75, 86+30*i, 30, 14);
				_numLabels[i].setFont(new java.awt.Font("Tahoma",1,14));
			}
			{
				_movieLabels = new JLabel[10];
				for (int i=0 ; i<10 ; i++){
					_movieLabels[i] = new JLabel();
					this.add(_movieLabels[i]);
					_movieLabels[i].setFont(new java.awt.Font("Tahoma",1,14));

					_movieLabels[i].setBounds(109, 86+30*i, 407, 14);

					final JLabel tmp = _movieLabels[i];
					_movieLabels[i].addMouseListener(new MouseAdapter() {
						public void mouseEntered(MouseEvent evt) {
							tmp.setForeground(Color.CYAN);

						}
						public void mouseExited(MouseEvent evt) {
							tmp.setForeground(Color.BLACK);
						}
						public void mouseReleased(MouseEvent evt){
							ToGuiFacade.getInstance().showMovieScreen(tmp.getText(),_user);
						}
					});
				}
			}
			{
				_scoresLabels = new JLabel[10];
				for (int i=0 ; i<10 ; i++){
					_scoresLabels[i] = new JLabel();
					this.add(_scoresLabels[i]);
					_scoresLabels[i].setFont(new java.awt.Font("Tahoma",1,14));

					_scoresLabels[i].setBounds(420, 86+30*i, 50, 14);
				}
			}
			{
				_rateButtons = new JButton[10];
				for (int i=0; i<10; i++){
					_rateButtons[i]=new JButton("rate");
					this.add(_rateButtons[i]);
					_rateButtons[i].setBounds(10, 86+30*i, 60, 14);
					final int tmpnum=i;
					_rateButtons[i].addMouseListener(new MouseAdapter() {
						public void mouseReleased(MouseEvent evt) {
							rateButtonActionPerformed(evt,tmpnum);
						}
					});
				}
			}
			{
				next10Button = new JButton();
				this.add(next10Button);
				next10Button.setText("Next 10 movies");
				next10Button.setBounds(243, 395, 191, 21);
				next10Button.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						next10ButtonActionPerformed(evt);
					}
				});
			}
			{
				previousButton = new JButton();
				this.add(previousButton);
				previousButton.setText("Previous 10 movies");
				previousButton.setBounds(28, 395, 191, 21);
				previousButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						previousButtonActionPerformed(evt);
					}
				});
			}
			{
				backButton = new JButton();
				this.add(backButton);
				backButton.setText("back");
				backButton.setBounds(625, 294, 63, 21);
				backButton.setBounds(616, 457, 72, 21);
				backButton.addMouseListener(new MouseAdapter() {
					public void mouseReleased(MouseEvent evt) {
						ToGuiFacade fac= ToGuiFacade.getInstance();
						fac.back();
					}
				});
			}
			logic.Facade.getInstance().getRecommendation(_user);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	protected void rateButtonActionPerformed(MouseEvent evt, int tmpnum) {
		String movie = _movieLabels[tmpnum].getText();
		ToGuiFacade fac= ToGuiFacade.getInstance();
		fac.changeScreen(new pnlAddRating(_user, movie));
	}

	public void setMovies(MovieComputedRating[] recs ,int begin){
		for (int i=0; i<10; i++){
			_movieLabels[i].setText("");
			_numLabels[i].setText("");
			_scoresLabels[i].setText("");
			_rateButtons[i].setVisible(false);
		}
		for (int i=0; i<recs.length&& i<10; i++){
			_movieLabels[i].setText(recs[i].get_movieName());
			_numLabels[i].setText((new Integer(begin+i+1)).toString());
			_rateButtons[i].setVisible(true);
			try{
				_scoresLabels[i].setText(recs[i].get_p().toString().substring(0, 4));
			}catch(Exception e){ //in case the string is small
				_scoresLabels[i].setText(recs[i].get_p().toString());
			}
		}
	}

	private void next10ButtonActionPerformed(ActionEvent evt) {
		//System.out.println("next10Button.actionPerformed, event="+evt);
		if(!_numLabels[9].getText().equals(""))
			logic.Facade.getInstance().nextTenMovies(_user,new Integer (_numLabels[9].getText()));

	}

	private void previousButtonActionPerformed(ActionEvent evt) {
		//System.out.println("jButton1.actionPerformed, event="+evt);
		int i =(new Integer (_numLabels[0].getText())-11);
		if (i<0)
			i=0;
		logic.Facade.getInstance().nextTenMovies(_user,i);

	}


}
